package mall

import (
	"context"
	"github.com/zeromicro/go-zero/core/logx"
	"private-domain-overseas-service/app/business/api/internal/svc"
	"private-domain-overseas-service/tools/ent"
	"private-domain-overseas-service/tools/ent/hzmallbrand"
	"private-domain-overseas-service/tools/ent/predicate"
)

type MallBrandDao struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewMallBrandDao(ctx context.Context, svcCtx *svc.ServiceContext) *MallBrandDao {
	return &MallBrandDao{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

// MallBrandConditionsAll - 根据条件查询多个品牌信息
func (l *MallBrandDao) MallBrandConditionsAll(conditions []predicate.HzMallBrand) ([]*ent.HzMallBrand, error) {
	brand, err := l.svcCtx.Orm.HzMallBrand.Query().
		Where(conditions...).
		Order(ent.Desc(hzmallbrand.FieldMallBrandSort)).
		All(l.ctx)
	if err != nil {
		return nil, err
	}
	return brand, nil
}

// MallBrandConditionsAllByPage - 分页查询品牌信息
func (l *MallBrandDao) MallBrandConditionsAllByPage(conditions []predicate.HzMallBrand, current, pageSize int64) ([]*ent.HzMallBrand, int64, error) {
	query := l.svcCtx.Orm.HzMallBrand.Query().Where(conditions...)

	total, err := query.Count(l.ctx)
	if err != nil {
		return nil, 0, err
	}

	records, err := query.
		Offset(int((current - 1) * pageSize)).
		Order(ent.Desc(hzmallbrand.FieldMallBrandSort)).
		Limit(int(pageSize)).
		All(l.ctx)
	if err != nil {
		return nil, 0, err
	}

	return records, int64(total), nil

}

// MallBrandConditionsFirst - 根据条件查询多个品牌信息
func (l *MallBrandDao) MallBrandConditionsFirst(conditions []predicate.HzMallBrand) (*ent.HzMallBrand, error) {
	brand, err := l.svcCtx.Orm.HzMallBrand.Query().
		Where(conditions...).
		First(l.ctx)
	if err != nil {
		return nil, err
	}
	return brand, nil
}
